package com.viviet.mobileservices.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Properties;

/**
 * Helpers class, which contains static methods helper methods like loading the given property file etc.
 */
public final class Helpers {

	// for logging
	private static final Logit m_log = Logit.getInstance(Helpers.class);
	private static final String SYSTEM_CLASS_PATH_PROPERTY_NAME =
		"java.class.path";
	private static final String SYSTEM_PATH_SEP_PROPERTY_NAME =
		"path.separator";
	private static String[] m_classPathSearchDirs = null;

	// default charset used in getUnicodeString
	private static final String DEFAULT_CHARSET = "SJIS";

	// singleton
	private Helpers() {
		;
	}

	/**
	 * Loads the given property file by searching the CLASSPATH or java.class.path system property value and returns the Properties object.
	 *
	 * @param propertyFileName  Name of the property file.
	 * @return Returns Properties object containing the contents of the specified Properties file.
	 * @exception   java.io.FileNotFoundException   Thrown if the given property file could not found in the CLASSPATH.
	 */
	public static Properties getProperties(String propertyFileName)
		throws java.io.FileNotFoundException {


		InputStream is = null;
		try {
			String configPath = System.getProperty("configPath");
			//System.out.println("configPath=" + configPath);
			File file = new File( configPath + "/" + propertyFileName );
			is = new FileInputStream( file );
			
			// load properties
			Properties props = new Properties();
			props.load(is);
			return props;

		} catch (Exception ignore) {
			ignore.printStackTrace();
			throw new java.io.FileNotFoundException(
				propertyFileName + " not found");
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * Loads the given property file by searching the CLASSPATH or java.class.path system property value and returns the Properties object.
	 *
	 * @param propertyFileName  Name of the property file.
	 * @param onNotFound Properties to return if the named properties file is not found.
	 * @return Returns Properties object containing the contents of the specified Properties file if found, or the onNotFound value if not found.
	 */
	public static Properties getProperties(
		String propertyFileName,
		Properties onNotFound) {
		try {
			return getProperties(propertyFileName);
		} catch (java.io.FileNotFoundException fe) {
			m_log.warn("Properties file not found: " + propertyFileName);
			return onNotFound;
		}
	}

	public static int getIntegerProperty(
		Properties p,
		String name,
		int defaultValue) {
		String l = p.getProperty(name);
		return l == null ? defaultValue : Integer.valueOf(l).intValue();
	}

	public static String getStringProperty(
		Properties p,
		String name,
		String defaultValue) {
		String propertyValue = p.getProperty(name);
		return propertyValue == null ? defaultValue : propertyValue;
	}

	public static boolean getBooleanProperty(
		Properties p,
		String name,
		boolean defaultValue) {
		String propertyValue = p.getProperty(name);
		return propertyValue == null
			? defaultValue
			: new Boolean(propertyValue).booleanValue();
	}

  public static String createStringId(String header, int n, long id) {
    NumberFormat idFormat = createNumberFormat( n );
    return header + idFormat.format( id );
  }
  public static String createStringId( String header, int n, int id ) {
    NumberFormat idFormat = createNumberFormat( n );
    return header + idFormat.format( id );
  }

  private static NumberFormat createNumberFormat( int n ) {
      NumberFormat idFormat = null;
      try {

      	StringBuffer sb =  new StringBuffer(""); 
      	for ( int i = 0; i < n; i++ ) {
      		sb = sb.append("0");
      	}
        idFormat = new DecimalFormat( sb.toString() );
      }
      catch ( Exception e ) {
        e.printStackTrace();
      }
      return idFormat;
  	}

	public static String getFullPath(String fileName)
			throws java.io.FileNotFoundException {
		try {
			String configPath = System.getProperty("configPath");
			if (configPath == null) {
				configPath = "";
			}
			//System.out.println("configPath=" + configPath);
			//System.out.println("File.separatorChar = " + File.separatorChar);
			return configPath + File.separatorChar + fileName;

		} catch (Exception ignore) {
			ignore.printStackTrace();
			throw new java.io.FileNotFoundException(fileName);
		}
		
	}
	
}
